import canUseDom from '../../util/dom/canUseDom'

export function getTransitionName(transitionName, transitionType) {
  if (!transitionName) return null
  if (typeof transitionName === 'object') {
    const type = transitionType.replace(/-\w/g, march => march[1]?.toUpperCase())
    return transitionName[type]
  }

  return `${transitionName}-${transitionType}`
}

const prefixedEventNames = {}

export function getVendorPrefixedEventName(eventName) {
  if (prefixedEventNames[eventName]) {
    return prefixedEventNames[eventName]
  }

  const prefixMap = vendorPrefixes[eventName]

  if (prefixMap) {
    const stylePropList = Object.keys(prefixMap)
    const len = stylePropList.length
    for (let i = 0; i < len; i += 1) {
      const styleProp = stylePropList[i]
      if (
        Object.prototype.hasOwnProperty.call(prefixMap, styleProp) &&
        styleProp in style
      ) {
        prefixedEventNames[eventName] = prefixMap[styleProp]
        return prefixedEventNames[eventName]
      }
    }
  }

  return ''
}

const internalAnimationEndName = getVendorPrefixedEventName('animationend')
const internalTransitionEndName = getVendorPrefixedEventName('transitionend')

export const supportTransition = !!(
  internalAnimationEndName && internalTransitionEndName
)